<template>
  <el-dialog :title="dialogTitle" :visible="showDialog" @close="close">
    <el-form :ref="'form'" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="form.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          type="textarea"
          placeholder="1-100个字符"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="btnOk">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-row>

      </el-form-item>
    </el-form>

  </el-dialog>

</template>

<script>
import { addDepartment, getDepartment, getDepartmentDetail, getDepartmentLeaders, updateDepartment } from '@/api/department'

export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      // 部门负责人列表
      managerList: [],
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let result = await getDepartment()
              // 判断是否是编辑模式
              if (this.form.id) {
                result = result.filter(item => item.id !== this.form.id)
              }
              if (result.some(item => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编号不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let result = await getDepartment()
              if (this.form.id) {
                result = result.filter(item => item.id !== this.form.id)
              }
              if (result.some(item => item.code === value)) {
                callback(new Error('部门编号已存在'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      // 当点击编辑后再点击新增,新增弹窗显示编辑
      this.form = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      this.managerList = await getDepartmentLeaders()
    },
    async btnOk() {
      this.$refs.form.validate()
      if (this.form.id) {
        await updateDepartment(this.form)
        this.$message.success('更新部门成功')
      } else {
        await addDepartment({ ...this.form, pid: this.currentNodeId })
        this.$message.success('新增部门成功')
      }
      this.$emit('updateDepartment')
      this.close()
    },
    // 获取组织的详情
    async getDepartmentDetail() {
      this.form = await getDepartmentDetail(this.currentNodeId)
    }
  }
}
</script>
